import IfCaptcha from "./captcha/index.vue";
import * as Vue from 'vue';

const getContainer = () => {
  let container = document.body
  // 微前端环境下
  if (window.__POWERED_BY_QIANKUN__) {
    container = document.querySelector('#micro-app > div')
  }
  return container
}

const install = function (app) {
  const if_instance = app.extend(IfCaptcha);
  let _ins_;
  const initInstance = () => {
    _ins_ = new if_instance();
    let el = _ins_.$mount().$el;
    getContainer().appendChild(el);
  }
  app.prototype.$ifCaptcha = {
    verify(options) {
      initInstance();
      return _ins_.captcha(options);
    }
  }
  app.component(IfCaptcha.name, IfCaptcha);
};

/* istanbul ignore if */
if (typeof window !== "undefined" && window.Vue) {
  install(window.Vue);
}

export const useCaptcha = () => {
  const { inject } = Vue;
  return inject('_ifCaptcha_micro')
}

export default { install };
